package com.luyuan.crm.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.user.PartyGroup;
import com.luyuan.crm.user.service.PartyGroupService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Component("partyGroupAction")
@Scope("view")
public class PartyGroupAction extends BaseAction {
    @Resource(name = "partyGroupService")
    private PartyGroupService partyGroupService;

    // 查询条件
    private PartyGroup criteria = new PartyGroup();

    // 添加或编辑模型
    private PartyGroup partyGroup;

    public PartyGroup getCriteria() {
        return criteria;
    }

    public PartyGroup getPartyGroup() {
        String userId = this.getRequestParameter("partyGroupId");
        if (userId != null && !"".equals(userId)) {
            partyGroup = partyGroupService.getById(userId);
        }
        if (partyGroup == null) {
            partyGroup = new PartyGroup();
        }
        return partyGroup;
    }

    public void setCriteria(PartyGroup criteria) {
        this.criteria = criteria;
    }

    public void setPartyGroup(PartyGroup partyGroup) {
        this.partyGroup = partyGroup;
    }

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder order : tableOrders) {
            if (order.toOrder() != null) {
                orders.add(order.toOrder());
            }
        }
        orders.add(Order.desc("id"));
        CriteriaTemplate c = CriteriaTemplate.forClass(PartyGroup.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("partyGroupName", criteria.getPartyGroupName()));
        Page page = partyGroupService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void addPartyGroup() {
        partyGroupService.addPartyGroup(partyGroup);

        addGlobalI18nMessage("crud_add_success_msg");
    }

    public void updatePartyGroup() {
        partyGroupService.updatePartyGroup(partyGroup);

        addGlobalI18nMessage("crud_update_success_msg");
    }

    public void removePartyGroup(PartyGroup partyGroup) {
        partyGroupService.removePartyGroup(partyGroup);

        addGlobalI18nMessage("数据删除成功！");
    }
}
